package com.example.barry.pdf.Net;


import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;


import com.example.barry.pdf.PgSQL.SFDCConf;
import com.example.barry.pdf.PgSQL.ServerInfo;
import com.example.barry.pdf.PgSQL.postgresSQL;
import com.example.barry.pdf.PgSQL.postgresSQLimpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by barry on 17-1-10.
 */

public class GetStationAsynTask extends AsyncTask<String,Void,List<Map<String,Object>>>{

    private postgresSQL pSql = new postgresSQLimpl();
    private List<Map<String, Object>> stationmaps = new ArrayList<Map<String, Object>>();
    Handler handler;
    private int programId;
    private int s;




    public GetStationAsynTask(Handler handler){
        this.handler = handler;
    }

    protected List<Map<String,Object>> doInBackground(String...params){
        //String key = params[0];
        programId = pSql.getProgramId(params[0], "Conduit_ShopFloorID");
        s = Integer.parseInt(params[1]);
        List<ServerInfo> serverInfo = pSql.getServerInfo(params[0], s, programId);
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        if (serverInfo.size() > 0) {
            for (int i = 0; i < serverInfo.size(); i++) {
                ServerInfo sInfo = serverInfo.get(i);
                List<SFDCConf> stationList = pSql.getStationId(params[0], sInfo.getServerId());
                if (stationList.size() > 0) {
                    next: for (int j = 0; j < stationList.size(); j++) {
                        SFDCConf sConf = stationList.get(j);
                        for (int k = 0; k < list.size(); k++) {
                            if (sConf.getClientname().equals(list.get(k).get("clientname"))) {
                                continue next;
                            }
                        }
                        Map<String, Object> m = new HashMap<String, Object>();
                        m.put("sfdcser_Id", sInfo.getServerId());
                        m.put("sfdcser_name", sInfo.getName());
                        m.put("clientname", sConf.getClientname());
                        m.put("serverid", sConf.getServerid());
                        list.add(m);

                    }
                }

            }

        }
      //  Log.i("StationList",""+list);
        return list;
    }

    protected void onPostExecute(List<Map<String, Object>> result) {
        Log.i("result", "" + result);
        Message msg = new Message();
        msg.what = 0x123;
        msg.obj  = result;
        handler.sendMessage(msg);
    }

}
